//
//  MyFilmCommentListViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2018/1/22.
//  Copyright © 2018年 Lwx. All rights reserved.
//
#define wantCommentBtnH 40
#define border 10

#import "MyFilmCommentListViewController.h"
#import "MyFilmCommentListCell.h"
#import "MyFilmCommentModel.h"
#import "FilmCommentViewController.h"
@interface MyFilmCommentListViewController ()<UITableViewDelegate, UITableViewDataSource, MyFilmCommentListCellDelegate>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIButton * wantCommentBtn;
@property (nonatomic, strong)MyFilmCommentModel * model;
@property (nonatomic, strong)NSMutableArray * dataArra;
@property (nonatomic, assign)int currentPage;
@end

@implementation MyFilmCommentListViewController(Networking)
- (void)getFilmCommentListData{
    NSDictionary * dic = @{
                           @"did":@(self.did),
                           @"type":@(2),
                           @"page":@(self.currentPage),
                           @"row":@(6)
                           };
    WS(ws)
    [self showprogressHUD];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/diary/diaryComment") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hiddenProgressHUD];
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
        }else{
            if ([obj[@"code"] integerValue] == 100000){
                if (ws.currentPage == 1) {
                    [self.dataArra removeAllObjects];
                }
                ws.model = [MyFilmCommentModel yy_modelWithDictionary:obj];
                [self.dataArra addObjectsFromArray:ws.model.data];
                [self.tableView reloadData];
            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}
@end

@implementation MyFilmCommentListViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"评论详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self.view addSubview:self.wantCommentBtn];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - init
- (NSMutableArray *)dataArra{
    if (!_dataArra) {
        _dataArra = [NSMutableArray array];
    }return _dataArra;
}

- (UIButton *)wantCommentBtn{
    if (!_wantCommentBtn) {
        _wantCommentBtn = [[UIButton alloc] init];
        _wantCommentBtn.frame = CGRectMake(border, SCREEN_HEIGHT - border - wantCommentBtnH - 64, SCREEN_WIDTH - 2*border, wantCommentBtnH);
        [_wantCommentBtn setTitle:@"我要评论" forState:UIControlStateNormal];
        _wantCommentBtn.titleLabel.font = DEF_FontSize_14;
        [_wantCommentBtn setBackgroundColor:getColor(@"ffbb02")];
        [_wantCommentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_wantCommentBtn setCornerRadius:8];
        [_wantCommentBtn addTarget:self action:@selector(wantCommentBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }return _wantCommentBtn;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.wantCommentBtn.top - border);
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.currentPage = 1;
            [self getFilmCommentListData];
        }];
        self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
            self.currentPage ++;
            [self getFilmCommentListData];
        }];
        [self.tableView.mj_header beginRefreshing];
    }return _tableView;
}
#pragma mark - delegate和dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cell";
    [self.tableView registerClass:[MyFilmCommentListCell class] forCellReuseIdentifier:cellID];
    MyFilmCommentListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [[MyFilmCommentListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.delegate = self;
    cell.modelData = self.dataArra[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArra.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    CGFloat labelBottomY = [self getLabelHeightWithText:((MyFilmCommentModelData *)self.dataArra[indexPath.row]).content width:SCREEN_WIDTH - 20 font:14];
//    return (labelBottomY + 50)/WIDTH_5S_SCALE;
//}

- (void)wantCommentBtnClick{
    NSLog(@"我要评论");
    FilmCommentViewController * filmCommentVC = [[FilmCommentViewController alloc] init];
    filmCommentVC.did = self.did;
    [self.navigationController pushViewController:filmCommentVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat labelBottomY = [self getLabelHeightWithText:((MyFilmCommentModelData *)self.dataArra[indexPath.row]).content width:SCREEN_WIDTH - 20 font:14];
//    if (((MyFilmCommentModelData *)self.dataArra[indexPath.row]).content != nil && ((MyFilmCommentModelData *)self.dataArra[indexPath.row]).content.length > 0) {
//        return labelBottomY + 180;
//    }
    return labelBottomY + 60;
}

//自适应Lable高度
- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    return rect.size.height;
}

@end
